//
//  TTAddFactoryAddressViewController.m
//  TaoTuo
//
//  Created by 刘广 on 2020/3/23.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTAddFactoryAddressViewController.h"
#import "TTAddAddressViewCell.h"

static NSString *cellID = @"cellID";

@interface TTAddFactoryAddressViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) NSArray *areaArray;

@end

@implementation TTAddFactoryAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"添加工厂";
    
    UIButton *btn = [UIButton createButtonWithTitle:@"保存" titleColor:kMainColor titleFont:[UIFont systemFontOfSize:15] backgroundColor:nil target:self action:@selector(saveAction)];
    btn.frame = CGRectMake(0, 0, 40, 44);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    [self setupDatas];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTAddAddressModel *model = self.dataArray[indexPath.row];
    TTAddAddressViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    [cell setModel:model areaArray:self.areaArray];
    return cell;
}

// 保存地址信息
- (void)saveAction
{
    TTAddAddressModel *areaModel = nil;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    for (TTAddAddressModel *model in self.dataArray) {
        if (model.content.length == 0) {
            NSString *str = [NSString stringWithFormat:@"%@未填写", [model.title removeBlank]];
            str = [str stringByReplacingOccurrencesOfString:@"：" withString:@""];
            [self.view makeToast:str duration:1 position:CSToastPositionCenter];
            return;
        } else {
            if (model.field == nil) {
                areaModel = model;
            } else {
                [dic setObject:model.content forKey:model.field];
            }
        }
    }
    NSArray *arr = [areaModel.content componentsSeparatedByString:@" "];
    [dic setObject:arr.firstObject forKey:@"param.province"];
    [dic setObject:arr[1] forKey:@"param.city"];
    NSString *areaStr = arr.count > 2 ? arr.lastObject : @"";
    [dic setObject:areaStr forKey:@"param.area"];
    
    NSString *urlStr = addFactoryAddress_api;
    if (self.addressModel) {
        [dic setObject:self.addressModel.Id forKey:@"param.receiveAddressId"];
        urlStr = updateFactoryAddress_api;
    }
    
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] postUrlString:urlStr parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
        
        
    } showLoadingInView:self.view];
    
}


- (void)setupDatas
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil];
    self.areaArray = [NSArray arrayWithContentsOfFile:path];
    
    NSString *areaStr = nil;
    if (self.addressModel) {
        areaStr = [NSString stringWithFormat:@"%@ %@ %@", self.addressModel.Province, self.addressModel.City, self.addressModel.Area];
    }
    
    [self.dataArray addObject:[TTAddAddressModel setTitle:@"工厂简称：" content:self.addressModel.ReceiverName placeholder:@"请输入工厂简称" canClick:NO field:@"param.receiverName"]];
    [self.dataArray addObject:[TTAddAddressModel setTitle:@"手机号码：" content:self.addressModel.ReceiverMobile placeholder:@"请输入工厂联系人手机号" canClick:NO field:@"param.receiverMobile" keyboardType:UIKeyboardTypeNumberPad]];
    [self.dataArray addObject:[TTAddAddressModel setTitle:@"所在地区：" content:areaStr placeholder:@"省、市、区" canClick:YES field:nil]];
    [self.dataArray addObject:[TTAddAddressModel setTitle:@"详细地址：" content:self.addressModel.Address placeholder:@"街道、楼牌号等" canClick:NO field:@"param.address"]];
    
    [self.tableView reloadData];
    
}


- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = UIColorHex(#F1F5F6);
        _tableView.rowHeight = 60;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TTAddAddressViewCell class]) bundle:nil] forCellReuseIdentifier:cellID];
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.mas_topLayoutGuideTop);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuideBottom);
        }];
    }
    return _tableView;
}



- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}



@end
